package com.uzai.console.service.cms.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.internal.$Gson$Preconditions;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.cms.customlink.CmsCustomLinkDto;
import com.uzai.console.dto.cms.customlink.CmsCustomLinkIdDto;
import com.uzai.console.dto.cms.customlink.CmsCustomLinkQuery;
import com.uzai.console.entity.CmsCustomLink;
import com.uzai.console.mapper.CmsCustomLinkMapper;
import com.uzai.console.mapper.CmsMerTbGoodsMapper;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.cms.CmsCustomLinkService;
import com.uzai.console.service.cms.CmsTbGoodsService;
import com.uzai.console.vo.cms.customlink.CmsCustomLinkVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class CmsCustomLinkServiceImpl implements CmsCustomLinkService {

    Logger logger = LoggerFactory.getLogger(CmsCustomLinkServiceImpl.class);

    @Autowired
    private CmsCustomLinkMapper cmsCustomLinkMapper;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    /**
     * 分页查询CMS自定义链接列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsCustomLinkVo
     */
    public Page<CmsCustomLinkVo> findCmsCustomLinkList(CmsCustomLinkQuery cmsCustomLinkQuery){
        //新建返回到页面的LIST对象
        List<CmsCustomLinkVo> cmsCustomLinkVoList = new ArrayList<>();
        //执行分页查询对象
        Page<CmsCustomLink> page = new Page<>();
        cmsCustomLinkQuery.convert(page);
        List<CmsCustomLink> cmsCustomLinkList = cmsCustomLinkMapper.findByList(page, cmsCustomLinkQuery);
        if(cmsCustomLinkList != null){
            for (CmsCustomLink cmsCustomLink : cmsCustomLinkList){
                CmsCustomLinkVo cmsCustomLinkVo = new CmsCustomLinkVo();
                BeanUtils.copyProperties(cmsCustomLink, cmsCustomLinkVo);
                cmsCustomLinkVoList.add(cmsCustomLinkVo);
            }
        }
        //返回到页面分页对象
        Page<CmsCustomLinkVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(cmsCustomLinkVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询CMS自定义链接详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public CmsCustomLinkVo findCmsCustomLink(CmsCustomLinkIdDto cmsCustomLinkIdDto){

        if(cmsCustomLinkIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        CmsCustomLink cmsCustomLink = cmsCustomLinkMapper.selectById(cmsCustomLinkIdDto.getId(), cmsCustomLinkIdDto.getMerId());
        if(cmsCustomLink == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该自定义链接不存在");
        }

        CmsCustomLinkVo cmsCustomLinkVo = new CmsCustomLinkVo();
        BeanUtils.copyProperties(cmsCustomLink, cmsCustomLinkVo);
        return cmsCustomLinkVo;
    }

    /**
     * 添加CMS自定义链接详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param cmsCustomLinkDto
     * @return null
     */
    @Transactional
    public void addCmsCustomLink(CmsCustomLinkDto cmsCustomLinkDto){

        if(StringUtils.isBlank(cmsCustomLinkDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入名称");
        }

        if(StringUtils.isBlank(cmsCustomLinkDto.getImg())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入图片");
        }

        if(StringUtils.isBlank(cmsCustomLinkDto.getUrl())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入链接");
        }

        CmsCustomLink cmsCustomLink = new CmsCustomLink();
        Integer now = DateUtil.getNowTime();
        cmsCustomLink.setId(IdWorker.getId());
        cmsCustomLink.setCreateTime(now);
        cmsCustomLink.setUpdateTime(now);
        cmsCustomLink.setMerId(cmsCustomLinkDto.getMerId());
        cmsCustomLink.setLocation(cmsCustomLinkDto.getLocation());
        cmsCustomLink.setName(cmsCustomLinkDto.getName());
        cmsCustomLink.setImg(cmsCustomLinkDto.getImg());
        cmsCustomLink.setType(Tools.getInteger(cmsCustomLinkDto.getType()));
        cmsCustomLink.setUrl(cmsCustomLinkDto.getUrl());
        //保存自定义链接
        cmsCustomLinkMapper.insertSelective(cmsCustomLink);
    }

    /**
     * 修改CMS自定义链接详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param cmsCustomLinkDto
     * @return null
     */
    @Transactional
    public void updateCmsCustomLink(CmsCustomLinkDto cmsCustomLinkDto){

        if(cmsCustomLinkDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        if(StringUtils.isBlank(cmsCustomLinkDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入名称");
        }

        if(StringUtils.isBlank(cmsCustomLinkDto.getImg())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入图片");
        }

        if(StringUtils.isBlank(cmsCustomLinkDto.getUrl())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入链接");
        }

        CmsCustomLink cmsCustomLink = cmsCustomLinkMapper.selectById(cmsCustomLinkDto.getId(), cmsCustomLinkDto.getMerId());

        if(cmsCustomLink == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该自定义链接不存在");
        }

        Integer now = DateUtil.getNowTime();
        cmsCustomLink.setUpdateTime(now);
        cmsCustomLink.setName(cmsCustomLinkDto.getName());
        cmsCustomLink.setImg(cmsCustomLinkDto.getImg());
        cmsCustomLink.setUrl(cmsCustomLinkDto.getUrl());
        cmsCustomLink.setType(Tools.getInteger(cmsCustomLinkDto.getType()));

        //保存自定义链接
        cmsCustomLinkMapper.updateByIdSelective(cmsCustomLink);
    }

    /**
     * 删除
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param cmsCustomLinkIdDto
     * @return null
     */
    @Transactional
    public void deleteCmsCustomLink(CmsCustomLinkIdDto cmsCustomLinkIdDto){

        if(cmsCustomLinkIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        CmsCustomLink cmsCustomLink = cmsCustomLinkMapper.selectById(cmsCustomLinkIdDto.getId(), cmsCustomLinkIdDto.getMerId());
        if(cmsCustomLink == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该自自定义链接不存在");
        }

        //删除自定义链接
        cmsCustomLinkMapper.deleteById(cmsCustomLinkIdDto.getId(), cmsCustomLinkIdDto.getMerId());
    }


    /**
     * 复制官方自定义链接
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return merId
     */
    public List<CmsCustomLinkVo> copyDefaultCmsCustomLink(Long merId){
        //新建返回到页面的LIST对象
        List<CmsCustomLinkVo> cmsCustomLinkVoList = new ArrayList<>();
        //先获取默认运营商id
        Long defaultMerId = uzaiConsoleSysConfig.getDefaultMerId();
        //查询默认账号的自定义配置
        CmsCustomLinkQuery cmsCustomLinkQuery = new CmsCustomLinkQuery();
        cmsCustomLinkQuery.setMerId(defaultMerId);
        List<CmsCustomLink> cmsCustomLinkList = cmsCustomLinkMapper.findByList(cmsCustomLinkQuery);
        if(cmsCustomLinkList != null && cmsCustomLinkList.size() > 0){
            for (CmsCustomLink cmsCustomLink : cmsCustomLinkList){

                //判断此自定义链接是否已经存在，如果存在则跳出
                CmsCustomLink cmsCustomLink_old = cmsCustomLinkMapper.selectByName(cmsCustomLink.getName(), merId);
                if(cmsCustomLink_old == null){//不存在同名得活动，则复制成功
                    cmsCustomLink.setId(IdWorker.getId());
                    cmsCustomLink.setMerId(merId);
                    cmsCustomLinkMapper.insertSelective(cmsCustomLink);
                    //返回给页面
                    CmsCustomLinkVo cmsCustomLinkVo = new CmsCustomLinkVo();
                    BeanUtils.copyProperties(cmsCustomLink, cmsCustomLinkVo);
                    cmsCustomLinkVoList.add(cmsCustomLinkVo);
                }
            }
        }
        return cmsCustomLinkVoList;
    }



}
